/**
 * author      : dormi330
 * date        : 2018/6/27
 * project     : mybatis
 * description : 这个Service 展示了 事务控制的一些坑
 */

package org.wzq.learn.tmp;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Repository
public class FoobarService {

    @Autowired
    FoobarRepository foobarRepository;

    /**
     * 事务控制是成功的
     */
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor = Exception.class)
    public void saveTwoA() throws Exception {
        foobarRepository.insert(Foobar.randomOne());
        foobarRepository.insert(Foobar.randomOne());
        float x = 10 / 0;
    }

    /**
     * 事务控制失败
     *
     * @throws Exception
     */
    public void saveTwoB() throws Exception {
        saveTwoFoobar();
    }

    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor = Exception.class)
    public void saveTwoFoobar() {

        foobarRepository.insert(Foobar.randomOne());
        foobarRepository.insert(Foobar.randomOne());
        float x = 10 / 0;
    }
}
